<?php

namespace App\Store\Controllers\Message;

use App\Admin\Models\SiteCompany;
use App\Common\Services\Store\Common as StoreCommon;
use App\Http\Controllers\Controller;
use App\Store\Models\Alisms\AliTemplate;
use App\Store\Models\Member\Member;
use App\Store\Models\Message\Festival;
use App\Store\Services\Message\AliSms;
use App\Store\Services\Message\Lianlu;
use Illuminate\Http\Request;
use App\Store\Validate\Message\Festival as ValidateMessageFestival;
use Illuminate\Support\Facades\Log;

class FestivalController extends Controller
{
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['title', 'store_ids', 'content']);
        $Validate = new ValidateMessageFestival('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $store_list = $req_data['store_ids'];
        $store_info_data = is_array($store_list) ? $store_list : (is_array(json_decode($store_list, true)) ? json_decode($store_list, true) : []);

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $data = [];
        foreach ($store_info_data as $k => $v) {
            $data[$k]['title'] = $req_data['title'];
            $data[$k]['store_id'] = $v;
            $data[$k]['content'] = $req_data['content'];
            $data[$k]['cp_id'] = $cp_id;
            $data[$k]['create_at'] = time();
        }
        Festival::insert($data);
        return response()->json(['code' => '000', 'msg' => '添加成功']);
    }

    public function getlist(Request $request)
    {
        $list = Festival::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateMessageFestival('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        Festival::where('id', $req_data['id'])->update([
            'is_del' => 2,
            'update_at' => time(),
        ]);
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }

    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'title', 'store_id', 'content']);
        $Validate = new ValidateMessageFestival('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        Festival::where('id', $req_data['id'])->update([
            'title' => $req_data['title'],
            'store_id' => $req_data['store_id'],
            'content' => $req_data['content'],
            'update_at' => time(),
        ]);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    public function send(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateMessageFestival('send');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $one = Festival::where(['id' => $req_data['id']])->first();
//        if (!$one || $one['status'] !== 1) {
//            return response()->json(['code' => '001', 'msg' => '发送失败，短信已发送']);
//        }

        if (empty($one)) {
            return response()->json(['code' => '001', 'msg' => '未获取活动模板']);
        }

        $one = $one->toArray();

        //查询用户手机
        $list = Member::where(['cp_id' => $one['cp_id'], 'store_id' => $one['store_id'],'is_del'=>1])->get()->toArray();
        if (!empty($list)) {
            $phone_arr = [];
            foreach ($list as $item) {
                $phone_arr[] = $item['mobile'];
            }
            $phone = array_values(array_unique($phone_arr));
            $companyinfo = SiteCompany::where([['id',$one['cp_id']],['is_open_sms',2]])->first();
            if(empty($companyinfo)){
                return response()->json(['code' => '001', 'msg' => '短信未开启']);
            }

            $res = Lianlu::NormalSend($phone, $one['content'], $companyinfo);

            Log::info('活动通知结果' . $res);
            $res = (array)json_decode($res, true);
            if (isset($res['message']) && $res['message'] == 'success') {
                Festival::where('id', $req_data['id'])->update([
                    'status' => 3,
                    'update_at' => time(),
                ]);
                return response()->json(['code' => '000', 'msg' => '发送成功']);
            }else{
                return response()->json(['code' => '001', 'msg' => '发送失败,'.$res['message']??'']);
            }

        }
        return response()->json(['code' => '001', 'msg' => '发送失败，用户为空']);
    }
}
